#!/usr/bin/env python
# -* coding: utf-8 -*-

#Author: Gustaw Lewandowski
#Licence : GPL

#import os
#import time
import urwid
from ucp import UrwidCmdProc, urwidcmd
from utils import ListWalkerBase, ListBoxBase, ListBoxSB, ListWalkerBase, ListWidget, utf8decode, divider, Border, log, setup

class ListWidgetBool(ListWidget):
	def __init__(self, label, initValue=True):
		self.val = initValue
		self.label = label
		w1 = urwid.Text(label)
		#w1 = urwid.AttrWrap(w1 , 'file', 'focus')
		self.w2 = urwid.Text(str(self.val))
		w = urwid.AttrWrap(self.w2 , 'dir', 'input2')
		#a1 = urwid.AttrSpec('#123', 'g30')
		#a2 = urwid.AttrSpec('#CCC,bold,underline', 'g50')
		#w1 = urwid.AttrWrap(w2 , a1, a2)
		#w2 = urwid.AttrMap(w2 , a1)
		#w1 = urwid.AttrMap(w1 , a1)
		w = urwid.Columns([w1, w], focus_column=1)
		w = urwid.AttrWrap(w , 'file', 'focus')
		self.__super.__init__(w)
	def toggle(self):
		self.val ^= True # xor
		self.w2.set_text(str(self.val))
	def keypress(self, (maxcol, ), key):
		""" Check keyboard for change option """
		if key in ("tab", "left", "right"):
			self.toggle()
		else:
			return key

class ListWidgetEdit(ListWidget):
	def __init__(self, label, initValue=""):
		self.val = initValue
		self.label = label
		w1 = urwid.Text(label)
		self.w2 = urwid.Edit("", self.val)
		w = urwid.AttrWrap(self.w2 , 'dir', 'input2')
		w = urwid.Columns([w1, w], focus_column=1)
#		w = urwid.Columns([w1, self.w2], focus_column=1)
		w = urwid.AttrWrap(w , 'file', 'focus')
		self.__super.__init__(w)
	def keypress(self, (maxcol,), key):
		self.w2.keypress((maxcol,), key)
		return key
	

class SetupWalker(ListWalkerBase):
	def __init__(self):
		self.lines = []
		self.readSetup()
	def readSetup(self):
		#for i in range(0, self.size):
		#self.lines.append(ListWidget(str(i)))
		self.lines.append(ListWidgetBool("doubleTitleBar", setup.doubleTitleBar))
		self.lines.append(ListWidgetBool("frameDirView", setup.frameDirView))
		#self.lines.append(ListWidgetEdit("logFile", setup.logFile))
		self.size = len(self.lines)

	def apply(self):
		""" Apply options """
		setup.doubleTitleBar = self.lines[0].val
		setup.frameDirView = self.lines[1].val
		#setup.logFile = self.lines[2].w2.get_edit_text()
		setup.apply()

class SetupPanel(urwid.AttrWrap):
	def __init__(self, old_body=None):
		#bt = urwid.BigText(('dialog', " Urwidcmd 0.1.2 "), urwid.HalfBlock5x4Font())
		#bt = urwid.Overlay(bt, bt1, 'center', None, 'middle', None)
		#bt = urwid.Filler(bt)
		self.old_body = old_body
		self.list = ListBoxBase(SetupWalker())
		self.__super.__init__(ListBoxSB(self.list), 'file')

	def keypress(self, (maxcol, maxrow), key):
		""" Check keyboard for exit """
		if key == 'f10':
			self.list.body.apply()
			return (urwidcmd, UrwidCmdProc.restore, self.old_body)
		elif key == 'esc':
			return (urwidcmd, UrwidCmdProc.restore, self.old_body)
		rc = self.__super.keypress((maxcol, maxrow), key)
		return rc

__all__ = ['SetupPanel']
